﻿using System;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

// Token: 0x020003B3 RID: 947
public class SaveMenuView : MonoBehaviour
{
	// Token: 0x060019A4 RID: 6564 RVA: 0x000A08E8 File Offset: 0x0009EAE8
	private void Initialize()
	{
		this.inputField = base.GetComponentInChildren<InputField>();
		Button[] componentsInChildren = base.GetComponentsInChildren<Button>();
		this.saveButton = componentsInChildren[0];
		this.saveButton.onClick.AddListener(new UnityAction(this.SaveFile));
		Button button = componentsInChildren[1];
		button.onClick.AddListener(new UnityAction(this.Close));
		this.buttonPrefab = Resources.Load<Button>("UI/Button/SceneItem");
		this.listCanvas = base.GetComponentInChildren<GridLayoutGroup>().transform;
		this.initialized = true;
	}

	// Token: 0x060019A5 RID: 6565 RVA: 0x000A0970 File Offset: 0x0009EB70
	private void ReloadSavedFiles()
	{
		string[] listSavedFiles = IOManager.Instance.GetListSavedFiles();
		Button[] componentsInChildren = this.listCanvas.GetComponentsInChildren<Button>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			UnityEngine.Object.Destroy(componentsInChildren[i].gameObject);
		}
		for (int j = 0; j < listSavedFiles.Length; j++)
		{
			Button button = UnityEngine.Object.Instantiate<Button>(this.buttonPrefab);
			button.transform.SetParent(this.listCanvas.transform, false);
			TextMeshProUGUI componentInChildren = button.GetComponentInChildren<TextMeshProUGUI>();
			string filename = listSavedFiles[j];
			filename = filename.Replace(".json", string.Empty);
			componentInChildren.text = filename;
			button.onClick.AddListener(delegate()
			{
				this.OnClickElement(filename);
			});
		}
	}

	// Token: 0x060019A6 RID: 6566 RVA: 0x000A0A54 File Offset: 0x0009EC54
	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.Return))
		{
			this.SaveFile();
		}
	}

	// Token: 0x060019A7 RID: 6567 RVA: 0x000A0A68 File Offset: 0x0009EC68
	private void OnEnable()
	{
		if (!this.initialized)
		{
			this.Initialize();
		}
		this.ReloadSavedFiles();
		this.inputField.text = string.Empty;
		this.inputField.ActivateInputField();
	}

	// Token: 0x060019A8 RID: 6568 RVA: 0x000A0A9C File Offset: 0x0009EC9C
	private void OnClickElement(string filename)
	{
		this.inputField.text = filename;
	}

	// Token: 0x060019A9 RID: 6569 RVA: 0x00069260 File Offset: 0x00067460
	private void Close()
	{
		base.gameObject.SetActive(false);
	}

	// Token: 0x060019AA RID: 6570 RVA: 0x000A0AAC File Offset: 0x0009ECAC
	private void SaveFile()
	{
		string text = this.inputField.text;
		if (text == null || text.Length < 2)
		{
			Debug.LogError("No filename included");
		}
		if (!text.EndsWith(".json"))
		{
			text += ".json";
		}
		Debug.Log("Saving scene as " + text);
		SavedScenesManager.Instance.SaveScene(text);
		this.Close();
	}

	// Token: 0x04001BAC RID: 7084
	private InputField inputField;

	// Token: 0x04001BAD RID: 7085
	private Button saveButton;

	// Token: 0x04001BAE RID: 7086
	private bool initialized;

	// Token: 0x04001BAF RID: 7087
	private Button buttonPrefab;

	// Token: 0x04001BB0 RID: 7088
	private Transform listCanvas;
}
